package com.xigua._树;

import com.xigua.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @date 2022/3/3111:40
 */
public class _98_验证二叉搜索树 {

    public boolean isValidBST(TreeNode root) {
        List<Integer> tmp = new ArrayList<>();
        midSearch(root,tmp);
        if (!tmp.isEmpty()){
            for (int i = 1; i < tmp.size(); i++) {
                if (tmp.get(i) <= tmp.get(i-1)) return false;
            }
        }
        return true;
    }

    void midSearch(TreeNode root,List<Integer> ans){
        if (root == null) return ;
        midSearch(root.left,ans);
        ans.add(root.val);
        midSearch(root.right,ans);

    }

}
